#include "gameselect.h"
#include "gamehall.h"
#include "gameroom.h"
#include <QIcon>
#include <QPushButton>
#include <QSound>
#include <QPainter>
#include <QPixmap>
#include <QFont>
#include <QFile>
#include <QTextEdit>
#include <QTextStream>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    // 设置“难度选择”界面的图标、名字
    this->setFixedSize(1080,720);
    this->setWindowIcon(QIcon(":Resource/icon.png"));
    this->setWindowTitle("难度选择");

    // 设置回退按钮
    QPushButton* pushButton_back = new QPushButton(this);
    pushButton_back->move(1000,640);
    pushButton_back->setGeometry(1000,640,60,60);
    pushButton_back->setIcon(QIcon(":Resource/back.png"));

    // 点击回退按钮回到上一页
    connect(pushButton_back,&QPushButton::clicked,[=](){
        this->close();
        GameHall* gameHall = new GameHall;
        gameHall->show();
        QSound::play(":Resource/clicked.wav");
    });

    QFont font("华文行楷",24);
    GameRoom* gameRoom = new GameRoom;

    // 设置选择难度按钮和历史战绩按钮
    QPushButton* pushButton_easy = new QPushButton(this);
    pushButton_easy->move(460,160);
    pushButton_easy->setGeometry(460,160,150,80);
    pushButton_easy->setText("简单模式");
    pushButton_easy->setFont(font);
    pushButton_easy->setStyleSheet("QPushButton{border:0px;color:white}");

    QPushButton* pushButton_normal = new QPushButton(this);
    pushButton_normal->move(460,280);
    pushButton_normal->setGeometry(460,280,150,80);
    pushButton_normal->setText("正常模式");
    pushButton_normal->setFont(font);
    pushButton_normal->setStyleSheet("QPushButton{border:0px;color:white}");

    QPushButton* pushButton_hard = new QPushButton(this);
    pushButton_hard->move(460,400);
    pushButton_hard->setGeometry(460,400,150,80);
    pushButton_hard->setText("困难模式");
    pushButton_hard->setFont(font);
    pushButton_hard->setStyleSheet("QPushButton{border:0px;color:white}");

    QPushButton* pushButton_record = new QPushButton(this);
    pushButton_record->move(460,520);
    pushButton_record->setGeometry(460,520,150,80);
    pushButton_record->setText("历史战绩");
    pushButton_record->setFont(font);
    pushButton_record->setStyleSheet("QPushButton{border:0px;color:white}");

    // 点击不同困难模式按钮进入游戏房间
    connect(pushButton_easy,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        QSound::play(":Resource/clicked.wav");
        gameRoom->setTimeout(300);
    });

    connect(pushButton_normal,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        QSound::play(":Resource/clicked.wav");
        gameRoom->setTimeout(200);
    });

    connect(pushButton_hard,&QPushButton::clicked,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        QSound::play(":Resource/clicked.wav");
        gameRoom->setTimeout(100);
    });

    // 设置历史战绩窗口
    connect(pushButton_record,&QPushButton::clicked,[=](){
        QWidget* widget = new QWidget;
        widget->setWindowTitle("历史战绩");
        widget->setWindowIcon(QIcon(":Resource/icon.png"));
        widget->setFixedSize(500,300);
        QSound::play(":Resource/clicked.wav");

        QTextEdit* edit = new QTextEdit(widget);
        edit->setFont(font);
        edit->setFixedSize(500,300);

        QFile file("D:/bite/C-program/project/Snake/gamedata.txt");
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);
        int data = in.readLine().toInt();
        edit->append("历史得分为：");
        edit->append(QString::number(data));

        widget->show();
    });
}

void GameSelect::paintEvent(QPaintEvent *event)
{
    (void) *event;
    QPainter painter(this);
    QPixmap pix(":Resource/game_select.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
